package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;

import com.aries.ui.view.title.TitleBarView;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.FragmentViewPagerAdapter;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.base.BaseMvpLazyLoadFragment;
import com.chexiehui.jc.entity.ForumInfo;
import com.chexiehui.jc.mvp.forum.ForumCovenant;
import com.chexiehui.jc.mvp.forum.ForumPresenter;
import com.chexiehui.jc.ui.activity.ForumCreateActivity;
import com.chexiehui.jc.ui.activity.ForumDynamicPostActivity;
import com.chexiehui.jc.utils.BundleBuilder;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.widget.LoadingLayout;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: SongJun.
 * CreateDate: 2019/1/21 0021.
 * Description: 论坛
 */

public class ForumFragment extends BaseMvpLazyLoadFragment<ForumPresenter> implements ForumCovenant.View {
    public static ForumFragment getInstance() {
        ForumFragment fragment = new ForumFragment();
        return fragment;
    }
    @BindView(R.id.loadingLayout)
    LoadingLayout loadingLayout;
    @BindView(R.id.titleBar)
    TitleBarView titleBar;
    @BindView(R.id.vp_forum_pager)
    ViewPager vp_forum_pager;
//    RadiusRelativeLayout rrl_search_panel;
    private SlidingTabLayout stl_forum_table;

    //private String[] titles = new String[]{"关注", "广场", "论坛", "奥迪A4L"};
    private String[] titles = null;
    private ArrayList<Fragment> forumFragmentList = new ArrayList<>();
    private List<ForumInfo> authForumList;
    private FragmentViewPagerAdapter pagerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_forum;
    }

    @Override
    protected void initTitleView(Bundle savedInstanceState) {
        View centerAction = View.inflate(mActivity, R.layout.layout_title_action_forum, null);
        stl_forum_table = centerAction.findViewById(R.id.stl_forum_table);
        titleBar.getTextView(Gravity.START).setVisibility(View.GONE);
        titleBar.setRightTextDrawable(R.mipmap.ic_home_add)
                .setOnRightTextClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        startActivityAnimBottom(ForumCreateActivity.class, new BundleBuilder()
                                .putBoolean("isFinishToBottom", true).create());
                    }
                })
                .setBgColor(ContextCompat.getColor(mActivity, R.color.colorWhite))
                .addCenterAction(titleBar.new ViewAction(centerAction));
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        loadingLayout.setStatus(LoadingLayout.Loading);
        forumFragmentList = new ArrayList<>();
        loadingLayout.setOnReloadListener(v -> {
            loadingLayout.setStatus(LoadingLayout.Loading);
            mvpPresenter.getAuthForums();
        });
        isPrepared = true;
        lazyLoad();

    }

    @Override
    protected void lazyLoad() {
        if(!isVisible || !isPrepared || mHasLoadedOnce){
            return;
        }
        mvpPresenter.getAuthForums();
    }

    private void setContentPage(){
        forumFragmentList.clear();
        if(authForumList != null && authForumList.size() > 0){
            titles = new String[3 + authForumList.size()];
            titles[0] = "关注";
            titles[1] = "广场";
            titles[2] = "论坛";
            forumFragmentList.add(ForumFollowFragment.getInstance());
            forumFragmentList.add(ForumSquareFragment.getInstance());
            forumFragmentList.add(ForumTribuneFragment.getInstance());
            for (int i = 0; i < authForumList.size(); i++) {
                titles[3 + i] = authForumList.get(i).getName();
                forumFragmentList.add(ForumFollowHomeFragment.getInstance(authForumList.get(i).getId()));
            }
        }else{
            titles = new String[3];
            titles[0] = "关注";
            titles[1] = "广场";
            titles[2] = "论坛";
            forumFragmentList.add(ForumFollowFragment.getInstance());
            forumFragmentList.add(ForumSquareFragment.getInstance());
            forumFragmentList.add(ForumTribuneFragment.getInstance());
        }
        stl_forum_table.setSnapOnTabClick(true);
        vp_forum_pager.setOffscreenPageLimit(titles.length);
        pagerAdapter = new FragmentViewPagerAdapter(getChildFragmentManager(), forumFragmentList, titles);
        vp_forum_pager.setAdapter(pagerAdapter);
        stl_forum_table.setViewPager(vp_forum_pager);
        stl_forum_table.setCurrentTab(1);
        vp_forum_pager.setCurrentItem(1);
    }

    @Override
    protected ForumPresenter createPresenter() {
        return new ForumPresenter(this);
    }

    @Override
    public void onGetAuthForumsSuccess(BaseModel<List<ForumInfo>> successResult) {
        mHasLoadedOnce = true;
        authForumList = successResult.getData();
        setContentPage();
        loadingLayout.setStatus(LoadingLayout.Success);
    }

    @Override
    public void onGetAuthForumsFailure(BaseModel<Object> failureResult) {
        if(failureResult.getResultCode() == Constants.PAGE_DATA_EMPTY){
            mHasLoadedOnce = true;
            authForumList = new ArrayList<>();
            setContentPage();
            loadingLayout.setStatus(LoadingLayout.Success);
        }else{
            loadingLayout.setStatus(LoadingLayout.Error);
            loadingLayout.setErrorText(failureResult.getResultInfo());
        }
    }

    @OnClick({R.id.fab_send_button})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.fab_send_button:
                if(vp_forum_pager.getCurrentItem() > 2){
                    int autIndex = vp_forum_pager.getCurrentItem() - 3;
                    if(authForumList != null && authForumList.size() > autIndex){
                        startActivityAnimBottom(ForumDynamicPostActivity.class, new BundleBuilder()
                                .putBoolean("isFinishToBottom", true)
                                .putSerializable("forumInfo", authForumList.get(autIndex)).create());
                    }else{
                        startActivityAnimBottom(ForumDynamicPostActivity.class);
                    }
                }else{
                    startActivityAnimBottom(ForumDynamicPostActivity.class);
                }
                break;
        }
    }

}